/****

### Step 8: Calculate Relationship Metrics

**Description:** Analyzes network statistics including relationship duration and competitive similarity measures.

- **Inputs:**
  - SCnetwork2
  - VTNIC_89_2019
  - TNIC3HHIdata_1989_2019

- **Outputs:**
> SC_rel_link
> SC_TNIC3HHI_P_by_supplier
> SC_rel_length_all
> SC_rel_length_all_directed
> VT_depth

***/


*** Examining Network Stats:

global dir your_path
cd "$dir"

use "SCnetwork2", clear

*** How many partners are new in a given year relative previous year? 

sort pairid fyear
xtset pairid fyear

keep if fyear>=2003

*** Create the number of years that a pair has a relationship with
by pairid: gen pair_years_rel = _N
sum pair_years_rel
	// Mean = 9.4 year relationships
tab source, sum(pair_years_rel)

*** Number of partners by year: 
egen gvkey1_partners = nvals(gvkey2), by(gvkey1 fyear)
egen total_num_firms = nvals(gvkey1), by(fyear)
gen  degree_cent = gvkey1_partners/(total_num_firms-1)
sum gvkey1_partners total_num_firms degree_cent
	// Mean = 71 partners, 1.1% average centrality 

*** How many of the 71 partners are new? 
sort pairid fyear
* Identify year relationship starts:
egen min_gvkey1_year=min(fyear), by(gvkey1)
egen max_gvkey1_year=max(fyear), by(gvkey1)

egen min_gvkey2_year=min(fyear), by(gvkey2)
egen max_gvkey2_year=max(fyear), by(gvkey2)


egen rel_start_year=min(fyear), by(pairid)
egen rel_end_year  =max(fyear), by(pairid)
sort gvkey1 gvkey2 fyear
gen  rel_length_run  = fyear - rel_start_year +1 
gen  rel_length  	 = rel_end_year - rel_start_year +1 
egen rel_length2 	 = nvals(fyear) , by(pairid)
*** How many years we are partners, scaled by the minimum of years we are connected:
gen gvkey1_sample_years=max_gvkey1_year-min_gvkey1_year+1
gen gvkey2_sample_years=max_gvkey2_year-min_gvkey2_year+1
gen rel_length_share = rel_length/min(gvkey1_sample_years, gvkey2_sample_years)
// table source, statistic(mean rel_length_share) nformat(%9.2f)

gen new_relationship = .
replace new_relationship = (fyear==rel_start_year & min_gvkey1_year<rel_start_year & fyear!=min_gvkey1_year)
egen total_new_rels = total(new_relationship), by(gvkey1 fyear)
egen sd_total_new_rels=sd(total_new_rels), by(gvkey1)
gen pct_new_relationships = total_new_rels/gvkey1_partners 
tab source, sum(pct_new_relationships)
	// Mean = 15.2% of connections are new (1.0 - 1.6% for Compustat and Factset, and 28% for VTNIC)

sort pairid fyear
gen firms_lost = (l.gvkey1_partners - gvkey1_partners) + total_new_rels if fyear!=2020

***
by pairid, sort: gen pairid_rowid=_n
by gvkey1 fyear, sort: gen gvkey_rowid=_n

******* STATISTICS to examine:
sort pairid fyear
sort gvkey1 fyear gvkey_rowid

*** % of partners lost by gorup
	gen partner_turnover=firms_lost/gvkey1_partners
	replace partner_turnover=0 if  firms_lost<0

save "SC_rel_link", replace // >3.2 million obs




use "SC_rel_link", clear
   drop if source==3 // Drop the VTNIC: 
   rename gvkey2 gvkey
   merge m:1 gvkey fyear using "TNIC3HHIdata_1989_2019.dta", keep(1 3) gen(_merge_TNIC_firm2) keepusing(tnic3tsimm tnic3hhi)
   rename (tnic3tsimm tnic3hhi) (P_tnic3tsimm P_tnic3hhi)
   foreach x in P_tnic3tsimm P_tnic3hhi {
      egen `x'_S = mean(`x'/ (supplier==1)), by(fyear gvkey1)
      egen `x'_C = mean(`x'/ (supplier==0)), by(fyear gvkey1)
   }
   keep gvkey1 fyear P_tnic3tsimm_S P_tnic3tsimm_C P_tnic3hhi_S P_tnic3hhi_C
   duplicates drop
   foreach x in P_tnic3tsimm_S P_tnic3tsimm_C P_tnic3hhi_S P_tnic3hhi_C {
        astile `x'_g2 = `x', nq(2)
   }
   rename gvkey1 gvkey
   save "SC_TNIC3HHI_P_by_supplier.dta", replace




**** Create the relative length of the relationships
use "SC_rel_link", clear
   collapse (mean) rel_length_run_v2 = rel_length_run , by(gvkey1 fyear)
   rename gvkey1 gvkey
save "SC_rel_length_all", replace 

use "SC_rel_link", clear
   collapse (mean) rel_length_run , by(gvkey1 supplier fyear)
   rename gvkey1 gvkey
   reshape wide rel_length_run , i(gvkey fyear) j(supplier)
   rename (rel_length_run0 rel_length_run1) (rel_length_run_C rel_length_run_S)
   merge 1:1 gvkey fyear using "SC_rel_length_all", keep(3) gen(_merge_SCall)
   sum 
save "SC_rel_length_all_directed", replace 





**** Creating VTNIC depth:
use "VTNIC_89_2019", clear
   drop if score==0
   br if gvkey1==1003 | gvkey2==1003
   gcollapse (nunique) VT_num_firms = gvkey2  ///
               (sum) VT_scores = score [pweight=score] , by(gvkey1 fyear)
   rename gvkey1 gvkey 
save "VT_depth", replace
